package cn.zwz.customer.controller;

import cn.hutool.core.util.IdUtil;
import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.customer.entity.Commodity;
import cn.zwz.customer.entity.Order;
import cn.zwz.customer.service.ICommodityService;
import cn.zwz.customer.service.IOrderService;
import cn.zwz.data.utils.ZwzNullUtils;
import cn.zwz.customer.entity.OrderItem;
import cn.zwz.customer.service.IOrderItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 熊英杰
 */
@Slf4j
@RestController
@Api(tags = "订单条目信息")
@RequestMapping("/zwz/orderItem")
@Transactional
public class OrderItemController {

    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private IOrderItemService iOrderItemService;

    @Autowired
    private ICommodityService iCommodityService;

    @RequestMapping(value = "/getByOrderId", method = RequestMethod.POST)
    @ApiOperation(value = "根据订单查询")
    public Result<List<OrderItem>> getByOrderId(@RequestParam String orderId) {
        QueryWrapper<OrderItem> qw = new QueryWrapper<>();
        qw.eq("order_id",orderId);
        return new ResultUtil<List<OrderItem>>().setData(iOrderItemService.list(qw));
    }

    @RequestMapping(value = "/inOrder", method = RequestMethod.POST)
    @ApiOperation(value = "添加进订单")
    public Result<Object> inOrder(@RequestParam String orderId,@RequestParam String commodityId,@RequestParam float number){
        Order order = iOrderService.getById(orderId);
        if(order == null) {
            return ResultUtil.error("订单不存在");
        }
        Commodity commodity = iCommodityService.getById(commodityId);
        if(commodity == null) {
            return ResultUtil.error("商品不存在");
        }
        OrderItem item = new OrderItem();
        item.setCode(IdUtil.getSnowflake(1, 1).nextId() + "");
        item.setCommodityId(commodity.getId());
        item.setCommodityName(commodity.getName());
        item.setUnit(commodity.getUnit());
        item.setMoney(commodity.getMoney());
        item.setDiscount(commodity.getDiscount());
        item.setNumber(BigDecimal.valueOf(number));
        item.setOrderId(orderId);
        iOrderItemService.saveOrUpdate(item);
        // 计算总订单金额
        calOrderSumNumber(order);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/editOrder", method = RequestMethod.POST)
    @ApiOperation(value = "编辑进订单")
    public Result<Object> editOrder(@RequestParam String id,@RequestParam String commodityId,@RequestParam float number){
        OrderItem item = iOrderItemService.getById(id);
        if(item == null) {
            return ResultUtil.error("条目不存在");
        }
        Order order = iOrderService.getById(item.getOrderId());
        if(order == null) {
            return ResultUtil.error("订单不存在");
        }
        Commodity commodity = iCommodityService.getById(commodityId);
        if(commodity == null) {
            return ResultUtil.error("商品不存在");
        }
        item.setCommodityId(commodity.getId());
        item.setCommodityName(commodity.getName());
        item.setUnit(commodity.getUnit());
        item.setMoney(commodity.getMoney());
        item.setDiscount(commodity.getDiscount());
        item.setNumber(BigDecimal.valueOf(number));
        iOrderItemService.saveOrUpdate(item);
        // 计算总订单金额
        calOrderSumNumber(order);
        return ResultUtil.success();
    }

    private void calOrderSumNumber(Order order) {
        QueryWrapper<OrderItem> itemQw = new QueryWrapper<>();
        itemQw.eq("order_id",order.getId());
        itemQw.eq("status",0);
        List<OrderItem> orderItemList = iOrderItemService.list(itemQw);
        BigDecimal moneySum = BigDecimal.ZERO;
        for (OrderItem orderItem : orderItemList) {
            moneySum = moneySum.add((orderItem.getMoney().subtract(orderItem.getDiscount())).multiply(orderItem.getNumber()));
        }
        order.setSumMoney(moneySum);
        iOrderService.saveOrUpdate(order);
    }

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条订单条目信息")
    public Result<OrderItem> get(@RequestParam String id){
        return new ResultUtil<OrderItem>().setData(iOrderItemService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部订单条目信息个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iOrderItemService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部订单条目信息")
    public Result<List<OrderItem>> getAll(){
        return new ResultUtil<List<OrderItem>>().setData(iOrderItemService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询订单条目信息")
    public Result<IPage<OrderItem>> getByPage(@ModelAttribute OrderItem orderItem ,@ModelAttribute PageVo page){
        QueryWrapper<OrderItem> qw = new QueryWrapper<>();
        if(!ZwzNullUtils.isNull(orderItem.getCreateBy())) {
        }
        IPage<OrderItem> data = iOrderItemService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<OrderItem>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改订单条目信息")
    public Result<OrderItem> saveOrUpdate(OrderItem orderItem){
        if(iOrderItemService.saveOrUpdate(orderItem)){
            return new ResultUtil<OrderItem>().setData(orderItem);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增订单条目信息")
    public Result<OrderItem> insert(OrderItem orderItem){
        iOrderItemService.saveOrUpdate(orderItem);
        return new ResultUtil<OrderItem>().setData(orderItem);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑订单条目信息")
    public Result<OrderItem> update(OrderItem orderItem){
        iOrderItemService.saveOrUpdate(orderItem);
        return new ResultUtil<OrderItem>().setData(orderItem);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除订单条目信息")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids) {
            OrderItem item = iOrderItemService.getById(id);
            if(item == null) {
                continue;
            }
            Order order = iOrderService.getById(item.getOrderId());
            iOrderItemService.removeById(id);
            // 计算总订单金额
            calOrderSumNumber(order);
        }
        return ResultUtil.success();
    }

    @RequestMapping(value = "/tui", method = RequestMethod.POST)
    @ApiOperation(value = "退货")
    public Result<Object> tui(@RequestParam String id){
        OrderItem item = iOrderItemService.getById(id);
        if(item == null) {
            return ResultUtil.error("条目不存在");
        }
        item.setStatus(1);
        iOrderItemService.saveOrUpdate(item);
        Order order = iOrderService.getById(item.getOrderId());
        // 计算总订单金额
        calOrderSumNumber(order);
        return ResultUtil.success();
    }
}
